import { createGuard } from "@/router/guard"
import NotFound from "@/views/common/layout/NotFound.vue"
import type { App } from "vue"
import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router"

const routes: Array<RouteRecordRaw> = [
  {
    path: "/login",
    name: "login",
    component: () => import("../views/common/login/index.vue")
  },
  { path: "/:pathMatch(.*)*", name: "NotFound", component: NotFound }
]

const routerContext = require.context("./module", true, /\.js$/)
//
console.log("route", routerContext.keys())

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      if (to.matched.every((record, i) => from.matched[i] !== record)) {
        return { left: 0, top: 0 }
      }
      return false
    }
  }
})

export function setupRouter(app: App<Element>) {
  app.use(router)
  createGuard(router)
}

// function isAuthenticated(): boolean {
//   const decoded: any = jwt.decode(store.state.token, { complete: true })
//   const isExpired = decoded && decoded.payload.exp - Math.round(Date.now() / 1000) < 0
//   if (store.state.token && !isExpired) {
//     return true
//   } else {
//     return false
//   }
// }

export default router
